package com.wfm.read;

import com.wfm.increment.DailyTimestampParamIncrementer;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.item.*;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;

import javax.sql.DataSource;
import java.util.List;

@SpringBootApplication
@EnableBatchProcessing
public class ItemReaderJob {

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    private DataSource dataSource;

    @Autowired
    JobBuilderFactory jobBuilderFactory;

    @Autowired
    StepBuilderFactory stepBuilderFactory;

    private int times = 10;

    public FlatFileItemReader<User> userItemReader(){
        return new FlatFileItemReaderBuilder<User>()
                .name("userItemReader")
                .resource(new ClassPathResource("user.txt"))
                .delimited().delimiter("#")
                .names("id", "name", "age")
                .recordSeparatorPolicy(new BlankLineRecordSeparatorPolicy())
                .targetType(User.class)
                .build();
    }


    public ItemProcessor itemProcessor(){
        return new ItemProcessor() {
            @Override
            public Object process(Object item) throws Exception {
                System.out.println("-------------process------------>");
                return item;
            }
        };
    }

    @Bean
    public UserPreStatementSetter preStatementSetter(){
        return new UserPreStatementSetter();
    }
    @Bean
    public JdbcBatchItemWriter<User> itemWriter(){
        return new JdbcBatchItemWriterBuilder<User>()
                .dataSource(dataSource)
                .sql("insert into user(id, name, age) values(?,?,?)")
                .itemPreparedStatementSetter(preStatementSetter())
                .build();
    }
//    public ItemWriter itemWriter(){
//        return new ItemWriter() {
//            @Override
//            public void write(List items) throws Exception {
//                System.out.println(items);
//            }
//        };
//    }

    @Bean
    public Step step1(){
        return stepBuilderFactory.get("step")
                .chunk(1)  //设置块的size为3次
                .reader(userItemReader())
                .processor(itemProcessor())
                .writer(itemWriter())
                .faultTolerant()
                .skipPolicy(new ItemSkipPolicy())
                .build();
    }

    @Bean
    public Job job(){
        return jobBuilderFactory.get("itemreader2-job")
                .incrementer(new DailyTimestampParamIncrementer())
                .start(step1())
                .build();
    }

    public static void main(String[] args) {
        SpringApplication.run(ItemReaderJob.class,args);
    }
}
